package defaulPackage;
import java.util.LinkedList;
import java.util.List;


public class RegisterUser {
	private static RegisterUser instance;
	private List<User> users;
	
	private RegisterUser() {
		users = new LinkedList<User>();
	}
	
	public static RegisterUser getInstance(){
		if (instance == null){
			instance = new RegisterUser();
		}
		return instance;
	}
	
	public void addUser(User user) {
		boolean checkUser = checkUser(user.getProfile());
		boolean checkEmail = checkEmail(user.getEmail());
		if (checkEmail && checkUser){
			System.out.println("Voc� receber� em breve um email de confirma��o, para finalizar seu cadastro");
			users.add(user);
		}else{
			System.out.println("Nick ou Email inv�lido!");
		}
	}
	
	public void removeUser(Profile profile){
		for(int x=0;x<users.size();x++){
			if (profile.match(users.get(x).getProfile())){
				System.out.println("Perfil removido com sucesso!");
				users.remove(x);
				break;
			}
		}
	}
	
	public Profile searchProfile(User user){
		for(User u: users){
			if (user.getProfile().match(u.getProfile())){
				return u.getProfile();
			}
		}
		return null;
	}
	
	public boolean login(Login login){
		for(User u : users){
			if (login.match(u.getLogin())){
				return true;
			}
		}
		return false;
	}
	
	private boolean checkUser(Profile profile){
		for(User u : users){
			if (profile.getNick().equals(u.getProfile().getNick())){
				return false;
			}
		}
		return true;
	}
	
	private boolean checkEmail(String email){
		for(User u : users){
			if (email.equals(u.getEmail())){
				return false;
			}
		}
		return true;
	}
	
	public List<User> getUsers() {
		return users;
	}

}
